﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevComponents.DotNetBar;
using System.Threading;

namespace Quan_Ly_Dai_Ly
{
    public partial class MainForm : Office2007Form
    {
        public MainForm()
        {
            InitializeComponent();
            Form f = new Welcome();
            f.StartPosition = FormStartPosition.CenterScreen;
            f.FormBorderStyle = FormBorderStyle.None;
            f.Show();
            Thread t = new Thread(TestConnect);
            t.Start();
            t.Join();
            Thread.Sleep(2000);
            f.Close();
        }

        private static void TestConnect()
        {
            connect = new SqlConnection("Data Source=(local);Initial Catalog=QLDL;Integrated Security=True");
            try
            {
                connect.Open();
                connect.Close();
            }
            catch (SqlException sqlEx)
            {
                connect.Close();
                MessageBoxEx.Show("Lỗi kết nối cơ sở dữ liệu!\n" + sqlEx.Message, "Thông Báo");
            }
        }

        public static int ExecProcedure(string nameProcedure, string[] nameParams, object[] valueParams)
        {
            int result = -1;
            try
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connect;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = nameProcedure;
                for (int i = 0; i < nameParams.Length; ++i)
                {
                    cmd.Parameters.AddWithValue(nameParams[i], valueParams[i]);
                }
                cmd.Parameters.Add("@ReturnValue", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
                connect.Open();
                cmd.ExecuteNonQuery();
                result = (int)cmd.Parameters["@ReturnValue"].Value;
                connect.Close();
            }
            catch (SqlException sqlEx)
            {
                connect.Close();
                MessageBoxEx.Show("Lỗi kết nối cơ sở dữ liệu!\n" + sqlEx.Message, "Thông Báo");
            }
            return result;
        }

        private static SqlConnection connect;
        public static SqlConnection Connect
        {
            get { return connect; }
        }

        private void tiếpNhậnĐạiLýToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new TiepNhanDaiLy();
            f.MdiParent = this;
            f.Show();
        }

        private void traCứuĐạiLýToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new TraCuuDaiLy();
            f.MdiParent = this;
            f.Show();
        }

        private void thêmLoạiĐạiLýToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new ThemLoaiDaiLy();
            f.MdiParent = this;
            f.Show();
        }

        private void thoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new ThongTinLoaiDaiLy();
            f.MdiParent = this;
            f.Show();
        }

        private void thêmQuậnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new ThemQuan();
            f.MdiParent = this;
            f.Show();
        }

        private void thêmMặtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new ThemMatHang();
            f.MdiParent = this;
            f.Show();
        }

        private void thôngTinCácMặtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new ThongTinMatHang();
            f.MdiParent = this;
            f.Show();
        }

        private void thêmĐơnVịTínhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new ThemDonViTinh();
            f.MdiParent = this;
            f.Show();
        }

        private void thôngTinCácQuậnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new ThongTinQuan();
            f.MdiParent = this;
            f.Show();
        }

        private void lậpPhiếuThuTiềnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new PhieuThuTien();
            f.MdiParent = this;
            f.Show();
        }

        private void xuấtHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new PhieuXuatHang();
            f.MdiParent = this;
            f.Show();
        }

        private void báoCáoDoanhSốToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new BaoCaoDoanhSo();
            f.MdiParent = this;
            f.Show();
        }

        private void báoCáoCôngNợĐạiLýToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new BaoCaoCongNo();
            f.MdiParent = this;
            f.Show();
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        private void giớiThiệuPhầnMềmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form f = new Welcome();
            f.ShowDialog();
        }

               
    }
}
